package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.BuyCarGoodsOutBean;
import com.diyunapp.xilinapp.myapi.BuyCarGoodsInfoChageInterface;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.diyunapp.xilinapp.utils.ScrollListViewDisplay;

import java.util.List;

/**
 * Created by NIDAYE on 2016/6/15.
 */
public class RecycleViewBuyCarAdapter extends RecyclerView.Adapter<RecycleViewBuyCarAdapter.MyViewHolder> implements View.OnClickListener{
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    public List<BuyCarGoodsOutBean> mDatas;
    public  Context myContext;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private ChangeGridLayoutManagerSpance changeGridLayoutManager;
    private boolean isAddFoot=false;
    private boolean isAddHead=false;

    private BuyCarGoodsInfoChageInterface outerInterface;

    //定义实现onItemClickClickListener 的接口
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface ChangeGridLayoutManagerSpance{
        public void change(int size, boolean isAddHead, boolean isAddFoot);
    }
    //提供接口给 让LayoutManager根据添加尾部 头部与否来做判断 显示头部与底部的SpanSize要在添加头部和尾部之后
    public void setChangeGridLayoutManager(ChangeGridLayoutManagerSpance changeGridLayoutManager){
        this.changeGridLayoutManager=changeGridLayoutManager;
        changeGridLayoutManager.change(getItemCount()-1,isAddHead,isAddFoot);
    }

    public RecycleViewBuyCarAdapter(List<BuyCarGoodsOutBean> mDatas, Context myContext, BuyCarGoodsInfoChageInterface outerInterface) {
        this.mDatas = mDatas;
        this.myContext = myContext;
        this.outerInterface = outerInterface;
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead=true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot=true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        Log.i("zhangxiaoxidebug","MyViewHolder onCreateViewHolder");
        View view = null;
        switch (i) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = View.inflate(viewGroup.getContext(), R.layout.buycar_out_list, null);
                view.setOnClickListener(this);
                break;

            case TYPE_FOOT:
                view =footView;
                break;
        }
        return new MyViewHolder(view,i);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        if(getItemViewType(position)==TYPE_ITEM)
        {
            Log.i("zhangxiaoxidebug","onBindViewHolder position="+position);
            int dataPosition = position - headViewSize;
            Log.i("zhangxiaoxidebug","onBindViewHolder dataPosition="+dataPosition);
            final BuyCarGoodsOutBean currentdata = mDatas.get(dataPosition);
            if(currentdata.isStoreSelected())
            {
                holder.storeSelectCB.setChecked(true);
                currentdata.setAllGoodsState(true);
            }
            else
            {
                holder.storeSelectCB.setChecked(false);
                currentdata.setAllGoodsState(false);
            }

            holder.storeSelectCB.setText(currentdata.getStoreName());
            Log.i("zhangxiaoxidebug","store id = "+currentdata.getStoreId());
            Log.i("zhangxiaoxidebug","store name = "+currentdata.getStoreName());
            final BuyCarInnerAdapter  innerAdapter= new BuyCarInnerAdapter(currentdata.getGoodsList(), myContext, new BuyCarGoodsInfoChageInterface() {
                @Override
                public void changeGoodsNum(int doType, int num) {

                    Log.i("zhangxiaoxidebug","RecycleViewBuyCarAdapter doType= "+doType);
                    Log.i("zhangxiaoxidebug","storeSelectCB state= "+holder.storeSelectCB.isChecked());
                    switch (doType)
                    {
                        //选中商品
                        case 1:
                            if(currentdata.ifAllGoodsSeleted())
                            {
                                if(!holder.storeSelectCB.isChecked())
                                {
                                    currentdata.onlyUI = true;
                                    holder.storeSelectCB.setChecked(true);
                                    currentdata.setStoreSelected(true);
                                }
                            }
                            holder.goods_totalprice_tv.setText("￥"+currentdata.getTotalPrice());
                            holder.goods_totalnum_tv.setText("共"+currentdata.getTotalNum()+"件");
                            break;
                        //取消选中商品
                        case 2:
                            if(holder.storeSelectCB.isChecked())
                            {
                                currentdata.onlyUI = true;
                                holder.storeSelectCB.setChecked(false);
                                currentdata.setStoreSelected(false);
                            }
                            holder.goods_totalprice_tv.setText("￥"+currentdata.getTotalPrice());
                            holder.goods_totalnum_tv.setText("共"+currentdata.getTotalNum()+"件");
                            break;
                        //商品数量修改
                        case 3:
                            //如果收到此消息，则说明商品当前处于选中状态，则更新本商铺的总数据信息
                            holder.goods_totalnum_tv.setText("共"+currentdata.getTotalNum()+"件");
                            holder.goods_totalprice_tv.setText("￥"+currentdata.getTotalPrice());
                            break;
                        //删除商品
                        case 4:
                            break;
                        default:
                            break;
                    }
                    outerInterface.changeGoodsNum(doType,num);
                }
            });

            holder.goods_lv.setAdapter(innerAdapter);
            holder.goods_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int goodid = FormatConfirm.stringToInt(currentdata.getGoodsList().get(position).getGoodId());
                    outerInterface.changeGoodsNum(5,goodid);
                }
            });
            Log.i("zhangxiaoxidebug","currentdata.getGoodsList() = "+currentdata.getGoodsList().size());
            innerAdapter.notifyDataSetChanged();
            Log.i("zhangxiaoxidebug","setListViewHeightBasedOnChildren()");
            ScrollListViewDisplay.setListViewHeightBasedOnChildren(holder.goods_lv);
            holder.goods_totalnum_tv.setText("共"+currentdata.getTotalNum()+"件");
            holder.goods_totalprice_tv.setText("￥"+currentdata.getTotalPrice());

            holder.storeSelectCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.i("zhangxiaoxidebug","onCheckedChanged = "+isChecked);
                    if(currentdata.onlyUI)
                    {
                        currentdata.onlyUI = false;
                    }
                    else
                    {
                        currentdata.setStoreSelected(isChecked);
                        currentdata.setAllGoodsState(isChecked);
                        innerAdapter.notifyDataSetChanged();
                    }
                }
            });
            //将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(dataPosition);
        }
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size() + headViewSize + footViewSize;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox storeSelectCB;
        ListView goods_lv;
        TextView goods_totalnum_tv;
        TextView goods_totalprice_tv;

        public MyViewHolder(View itemView,int type) {
            super(itemView);
            //当View是itemview时再执行此初始化操作
            Log.i("zhangxiaoxidebug","MyViewHolder out");
            if(type == TYPE_ITEM )
            {
                Log.i("zhangxiaoxidebug","MyViewHolder");
                storeSelectCB = (CheckBox)itemView.findViewById(R.id.store_select_cb);
                goods_lv =(ListView) itemView.findViewById(R.id.store_goodslist_incar_lv);
                goods_totalnum_tv =(TextView)itemView.findViewById(R.id.store_totalgoodnum_tv);
                goods_totalprice_tv =(TextView)itemView.findViewById(R.id.store_totalgoodprice_tv);
            }

        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



